<?php

require_once 'doctrine-orm/Doctrine/ORM/Tools/SchemaTool.php';
/**
 * Generates new schema and updates database
 * TODO: need to be removed on production
 *
 * @uri /utils/updatedb
 * @author alexey.ogarkov
 */
class UpdateDBResource extends Resource {

    public function get($request) {
        $em = EntityManagerFactory::getEntityManager();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
        $classes =  array(
          $em->getClassMetadata('Game'),
          $em->getClassMetadata('Board'),
          $em->getClassMetadata('TurnUpdate'),
          $em->getClassMetadata('Player')
        );
        $tool->dropDatabase();
        $tool->createSchema($classes);
        
        $response = new Response($request);
        $response->code = Response::OK;
        $response->addHeader('Content-type', 'text/plain');        
        $response->body =
                "Schema updated\n" .
                "\n" .
                "Classes:\n" .
                join($classes, "\n\n") . "\n" .
                "\n" .
                "Schema SQL:\n" .
                join($tool->getCreateSchemaSql($classes), "\n\n") . "\n" .
                "\n" .                
                "This request:\n" .
                "\n" .
                $request->__toString() . "\n" .
                "\n" .
                "This response:\n" .
                "\n" .
                $response->__toString();
        return $response;
    }

}

?>
